﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.DAO;
using System.Data.SqlClient;
using fideeraSoft.Utils;
using fideeraSoft.Entities;

namespace fideeraSoft.DAOimpl
{
    public class EnvasadoDAOSQLimpl:EnvasadoDAO
    {

        #region BUSCAR ULTIMO ID DE ENVASADO
        public int buscarNuevoUltimoIdEnvasadoEnBD()
        {
            int ID = 0;
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "SELECT TOP 1 ID FROM Envasado ORDER BY ID DESC ";
                SqlCommand cmd = new SqlCommand(query, conn);

                SQLclass.commandQuery(conn, cmd, query);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {

                    while (reader.Read())
                    {
                        ID = reader.GetInt32(0);
                        //  CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] ID : " + ID, CLogger.ERROR);      //DEBUG
                    }
                    ID++;
                }
                else
                {
                    ID = 1;
                }
            }
            catch (Exception e)
            {
                CLogger.Append("[buscarNuevoUltimoIdEnvasadoEnBD] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[buscarNuevoUltimoIdEnvasadoEnBD] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
            // CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] RETURN ID : " + ID, CLogger.ERROR);      //DEBUG
            return ID;
        }

        #endregion

      

        #region ALTA ENVASADO
        public void altaEnvasado(Envasado envasado)
        {
            SqlConnection conn = null;
            try
            {
                conn = SQLclass.conectarSQL();
                conn.Open();

                string query = "SET IDENTITY_INSERT Envasado ON; INSERT INTO Envasado (ID,codigo,produccion,fechaInicio,observacion) VALUES (@IdEnvasado,@Codigo,@Produccion,@FechaInicio,@Observacion)";
                
                SqlCommand cmd = new SqlCommand(query, conn);
                
                cmd.Parameters.AddWithValue("@IdEnvasado", envasado.IdEnvasado);
                cmd.Parameters.AddWithValue("@Codigo", envasado.Codigo);

                cmd.Parameters.AddWithValue("@Produccion", envasado.Produccion);

                cmd.Parameters.AddWithValue("@FechaInicio",envasado.FechaInicio);
                
                cmd.Parameters.AddWithValue("@Observacion", envasado.Observacion);

                cmd.ExecuteNonQuery();
                //cmd.Parameters.Clear();
                // cmd.CommandText = "SELECT @@IDENTITY";
                // int insertID=Convert.ToInt32(cmd.ExecuteScalar());

                //  cmd.Dispose();
                cmd = null;             

            }
            catch (Exception e)
            {
                CLogger.Append("[altaEnvasado] Exception alta: " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[altaEnvasado] Exception alta: " + e);
            }
            finally
            {
                conn.Close();
            }

        }

        #endregion
        

        #region UPDATE FECHA FIN DE PRODUCCION
        public void updateFechaFinEnvasadoPorId(string id, DateTime dateTimeFin)
        {

            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "UPDATE Envasado SET fechaFin=@FechaFin WHERE ID=@Id";

                SqlCommand cmd = new SqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@FechaFin", dateTimeFin);
                cmd.Parameters.AddWithValue("@Id", id);


                cmd.ExecuteNonQuery();

                cmd = null;
            }
            catch (Exception e)
            {
                CLogger.Append("[updateFechaFinEnvasadoPorId] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[updateFechaFinEnvasadoPorId] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
            // CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] RETURN ID : " + ID, CLogger.ERROR);      //DEBUG

        }

        #endregion
        

        #region UPDATE CANTIDAD DE PAQUETES ENVASADOS
        public void updateCantidadPaquetesEnvasadosPorId(string id, string cantPaq)
        {

            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "UPDATE Envasado SET cantidadPaquetes=@CantidadPaquetes WHERE ID=@Id";

                SqlCommand cmd = new SqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@CantidadPaquetes", cantPaq);
                cmd.Parameters.AddWithValue("@Id", id);


                cmd.ExecuteNonQuery();

                cmd = null;
            }
            catch (Exception e)
            {
                CLogger.Append("[updateCantidadPaquetesEnvasadosPorId] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[updateCantidadPaquetesEnvasadosPorId] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
            // CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] RETURN ID : " + ID, CLogger.ERROR);      //DEBUG

        }

        #endregion

        
        #region UPDATE OBSERVACION FIN ENVASADO POR ID ENVASADO
        public void updaceObservacionFinEnvasadoEnEnvasadoPorId(string idEnvasado, string observacionFin)        
        {

            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "UPDATE Envasado SET observacionFin=@ObservacionFin WHERE ID=@Id";

                SqlCommand cmd = new SqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@ObservacionFin", observacionFin);
                cmd.Parameters.AddWithValue("@Id", idEnvasado);


                cmd.ExecuteNonQuery();

                cmd = null;
            }
            catch (Exception e)
            {
                CLogger.Append("[updaceObservacionFinEnvasadoEnEnvasadoPorId] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[updaceObservacionFinEnvasadoEnEnvasadoPorId] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
            // CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] RETURN ID : " + ID, CLogger.ERROR);      //DEBUG

        }


        
        #endregion

        #region Eliminar ENVASADO POR ID

        public void eliminarEnvasadoPorId(int id)
        {
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "DELETE FROM Envasado WHERE ID='" + id.ToString() + "'";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                CLogger.Append("[eliminarEnvasadoPorId] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[eliminarEnvasadoPorId] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion


        #region Eliminar TODOS LOS ENVASADOS EN DB

        public void eliminarTodosLosEnvasadosEnDb()
        {
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "DELETE FROM Envasado";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                CLogger.Append("[eliminarTodosLosEnvasadosEnDb] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[eliminarTodosLosEnvasadosEnDb] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion
    }
}
